﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using DAL;
using BLL;
namespace Web.Admin
{
    public partial class SysAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断当前角色对象是否有相应的权限
            Role role = Session["role"] as Role;//从session取角色对象
            if (role != null && role.Authority != null && !role.Authority.Contains("SysAdd"))
            {
                //没权限跳转到提示页面或首页
                Response.Redirect("/Admin/Default.aspx");
            }
            //获取网址参数id
            object id = Request.QueryString["id"];
            if (id != null && !IsPostBack)
            {//修改
                //根据id从数据库取数据
                Sys o = SysBll.SelectOne(int.Parse(id.ToString()));
                //显示到界面
                this.TextBox_Name.Text = o.Name;



            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //第一步获取数据
            string Name = this.TextBox_Name.Text;
            string logo = this.Image1.ImageUrl;
            //第二步  创建实体类对想
            Sys obj = new Sys();
            obj.Name = Name;
            obj.Logo = logo;
            //获取网址参数id
            object id = Request.QueryString["id"];
            if (id != null)
            {//修改
                obj.Id = int.Parse(id.ToString());//id赋值
                bool r = SysBll.Update(obj);
                if (r)
                {//修改成功
                    Response.Redirect("/Admin/SysList.aspx");
                }
                else
                {
                    Response.Write("<script>alert('修改失败')</script>");
                }
            }
            else//添加
            {
                //第三步调用 业务类的添加方法
                bool r = SysBll.Add(obj);
                if (r)
                {//添加成功
                    Response.Redirect("/Admin/SysList.aspx");
                }
                else
                {
                    Response.Write("<script>alert('添加失败')</script>");
                }
            }

        }

        protected void Button_upload_Click(object sender, EventArgs e)
        {  //判断是否选择了文件
            if (this.FileUpload1.HasFile)
            {
                //获取文件名
                string filename = this.FileUpload1.FileName;
                //.的位置
                int pos = filename.LastIndexOf(".");
                //获取后缀q
                string suff = filename.Substring(pos + 1);
                if ("jpg|png|gif|jpeg".Contains(suff))
                {
                    //网站根目录物理路径
                    string path = Server.MapPath("/");
                    //保存地址
                    string path2 = path + "//upload//" + filename;// path + filename;
                    //实现保存
                    this.FileUpload1.SaveAs(path2);
                    this.Image1.ImageUrl = "/upload/" + filename;
                    //显示图片
                    // this.Image1.ImageUrl = "~/Images/2013_login4.jpg";
                    // this.FileUpload1. = "上传成功";
                }
                else
                {
                    //this.Label1.Text = "格式错误";
                }

            }
            else
            {
                //  this.Label1.Text = "请选择文件";
            }

        }
    }
}